﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Arcadia.Bookasa.Common.Service;
using Arcadia.Bookasa.Common.Watch;
using Arcadia.Bookasa.Data.Watching;

namespace Arcadia.Bookasa.Service.SourceMonitor
{
    public class FileSystemMonitor : BaseServiceExtension, IServiceExtension, ISourceWatcher
    {
        protected FileSystemWatcher _watcher = new FileSystemWatcher();

        public bool IsAcceptedSource(WatchSource source)
        {
            return true;
        }

        public int Watching(WatchSource source)
        {
            this._watcher.Path = source.SourceUri.AbsolutePath;
            this._watcher.Created += new FileSystemEventHandler(OnCreated);
            this._watcher.Deleted += new FileSystemEventHandler(OnDeleted);
            this._watcher.Renamed += new RenamedEventHandler(OnRenamed);
            this._watcher.EnableRaisingEvents = true;
            this._watcher.IncludeSubdirectories = true;

            return 0;
        }

        public void RegisterEvent(SourceChangedEventHandler handler)
        {
        }

        public void UnegisterEvent(SourceChangedEventHandler handler)
        {
        }

        private static void OnCreated(object source, FileSystemEventArgs e)
        {
            Console.WriteLine("文件新建事件处理逻辑 {0}  {1}  {2}", e.ChangeType, e.FullPath, e.Name);
        }

        private static void OnDeleted(object source, FileSystemEventArgs e)
        {
            Console.WriteLine("文件删除事件处理逻辑{0}  {1}   {2}", e.ChangeType, e.FullPath, e.Name);
        }

        private static void OnRenamed(object source, RenamedEventArgs e)
        {
            Console.WriteLine("文件重命名事件处理逻辑{0}  {1}  {2}", e.ChangeType, e.FullPath, e.Name);
        }
    }
}
